package com.huilian.iotbox.admin.controller;

import com.huilian.iotbox.admin.service.DeviceAnnualFeeService;
import com.huilian.iotbox.admin.service.UserService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.controller.BaseController;
import com.huilian.iotbox.data.dto.CommodityStockRecordDto;
import com.huilian.iotbox.data.dto.DeviceAnnualFeeDto;
import com.huilian.iotbox.data.enums.RoleEnum;
import com.huilian.iotbox.data.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author LinYinDong
 * @date 2024/7/19 上午10:04
 */
@RestController
@RequestMapping("/api/deviceAnnualFee")
public class DeviceAnnualFeeController extends BaseController {
    @Autowired
    private UserService userService;
    @Autowired
    private DeviceAnnualFeeService deviceAnnualFeeService;
    @PostMapping("/findDeviceAnnualFeeList")
    public ServerResponse findDeviceAnnualFeeList(HttpServletRequest request, @RequestBody DeviceAnnualFeeDto deviceAnnualFeeDto) {
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        UserInfo userInfo = userService.getLoginUserInfo(request);
        deviceAnnualFeeDto.setUserInfo(userInfo);
        deviceAnnualFeeDto.setPageNum(Integer.parseInt(pageNum));
        deviceAnnualFeeDto.setPageSize(Integer.parseInt(pageSize));
        return ServerResponse.createBySuccess(deviceAnnualFeeService.findDeviceAnnualFeeList(deviceAnnualFeeDto));
    }

    @DeleteMapping("/delete/{id}")
    public ServerResponse delete(@PathVariable Integer id,HttpServletRequest request){
        UserInfo userInfo = userService.getLoginUserInfo(request);
        if (!RoleEnum.SUPER_ADMINISTRATOR.getRoleId().equals(userInfo.getRoleId())) {
            if (userInfo.getPayeeUserId() == 0) {
                return ServerResponse.createByErrorMessage("您没有权限处理，请联系管理员开通权限");
            }
        }
        return ServerResponse.createBySuccess(deviceAnnualFeeService.delete(id));
    }
}
